package com.uniandes.edu.co.interfaz.ciclo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import com.uniandes.edu.co.dto.Tsp;
import com.uniandes.edu.co.excepciones.ExcepcionExiste;
import com.uniandes.edu.co.excepciones.ExcepcionParseInt;
import com.uniandes.edu.co.excepciones.ExcepcionVacio;
import com.uniandes.edu.co.interfaz.mundo.Principal;




/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
//public class InterfazCiclo extends javax.swing.JFrame {
public class InterfazCiclo extends JFrame {


	private JScrollPane jScrollPane1;
	private JButton jButton4;
	private JButton jButton3;
	private JButton jButton1;
	private JButton jButton5;
	private JButton jButton2;
	private JLabel jLabel4;
	private JLabel jLabel1;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				InterfazCiclo inst = new InterfazCiclo();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	public InterfazCiclo() {
		super();
		initGUI();
	}

	public InterfazCiclo(Principal id) {
		setTitle( "Menu Persona" );
		initGUI();
	}


	private void initGUI() {

		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				final DefaultTableModel dm = new DefaultTableModel();

				dm.setDataVector(Tsp.getSingleton().tablaCiclos(), Tsp.getSingleton().encabezadosCiclo());

				TableModel jTable1Model =dm;
				JTable jTable1 = new JTable(dm) {
					public void tableChanged(TableModelEvent e) {
						super.tableChanged(e);
						repaint();
					}
				};

				ButtonGroup group1 = new ButtonGroup();
				for (int i = 0; i < dm.getRowCount(); i++)
				{
					group1.add( (AbstractButton) dm.getValueAt(i, 0));
				}

				jTable1.setModel(jTable1Model);
				jTable1.getColumn("Seleccionar").setCellRenderer(
						new RadioButtonRenderer());
				jTable1.getColumn("Seleccionar").setCellEditor(
						new RadioButtonEditor(new JCheckBox()));
				jScrollPane1.setBounds(45, 146, 386, 228);
				jScrollPane1.setViewportView(jTable1);
				jTable1.setBounds(135, 207, 383, 225);


				{
					jButton4 = new JButton();
					getContentPane().add(jButton4);
					jButton4.setText("Eliminar");
					jButton4.setBounds(240, 99, 88, 23);
					jButton4.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {

							boolean bandera=true;
							for (int i = 0; i < dm.getRowCount(); i++)
							{
								if (((AbstractButton) dm.getValueAt(i, 0)).isSelected())
								{
									try {
										Tsp.getSingleton().eliminarCiclo(String.valueOf(dm.getValueAt(i, 1)));
										bandera=false;
										dispose();
										JOptionPane.showMessageDialog(null, "Se ha eliminado correctamente un Ciclo");
										InterfazCiclo.main(null);
									} catch (ExcepcionExiste e) {
										JOptionPane.showMessageDialog(null, e.getMessage());									
									} catch (ExcepcionVacio e) {
										JOptionPane.showMessageDialog(null, e.getMessage());
									}catch (ExcepcionParseInt e) {
										JOptionPane.showMessageDialog(null, e.getMessage());
									}

								}
							}
							if (bandera)
								JOptionPane.showMessageDialog(null, "Seleccione un elemento");

						}
					});
				}
				{
					jButton3 = new JButton();
					getContentPane().add(jButton3);
					jButton3.setText("Modificar");
					jButton3.setBounds(341, 100, 90, 21);
					jButton3.addActionListener(new ActionListener() {
						private boolean bandera=true;

						public void actionPerformed(ActionEvent evt) {

							for (int i = 0; i < dm.getRowCount(); i++)
							{
								if (((AbstractButton) dm.getValueAt(i, 0)).isSelected())
								{
									dispose();                               
									InterfazModificarCiclo.main(null, String.valueOf(dm.getValueAt(i, 1)));
									dispose();
									bandera=false;
								}
							}
							if (bandera){
								JOptionPane.showMessageDialog(null, "Seleccione un elemento");
							}
						}
					});
				}

				{
					jButton2 = new JButton();
					getContentPane().add(jButton2);
					jButton2.setText("Consultar");
					jButton2.setBounds(341, 60, 91, 21);
					jButton2.addActionListener(new ActionListener() {
						private boolean bandera=true;

						public void actionPerformed(ActionEvent evt) {
							for (int i = 0; i < dm.getRowCount(); i++)
							{
								if (((AbstractButton) dm.getValueAt(i, 0)).isSelected())
								{
									dispose();                               
									InterfazConsultarCiclo.main(null, String.valueOf(dm.getValueAt(i, 1)));
									dispose();
									bandera=false;
								}
							}
							if (bandera){
								JOptionPane.showMessageDialog(null, "Seleccione un elemento");
							}
						}
					});
				}

			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("CICLO");
				jLabel1.setBounds(46, 12, 386, 26);
				jLabel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				jLabel1.setFont(new java.awt.Font("Tahoma",1,14));
				jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
				jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
			}


			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4);
				jLabel4.setBounds(45, 86, 386, 8);
				jLabel4.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("Agregar");
				jButton1.setBounds(240, 60, 90, 21);
				jButton1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						InterfazAgregarCiclo.main(null,null);
						dispose();

					}
				});
			}

			{
				jButton5 = new JButton();
				getContentPane().add(jButton5);
				jButton5.setText("Regresar");
				jButton5.setBounds(340, 394, 91, 21);
				jButton5.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
						Principal.main(null);
					}
				});
			}


			pack();
			this.setSize(477, 465);
		} catch (Exception e) {
			//add your error handling code here
			e.printStackTrace();
		}
	}



}
